package com.itheima.manager.web;

import com.itheima.manager.pojo.Contact;
import com.itheima.manager.service.IAddService;
import com.itheima.manager.service.imp.AddServiceImp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/AddServlet.do")
public class AddServlet extends HttpServlet {

    private IAddService iAddService = new AddServiceImp();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String address = request.getParameter("address");
        String qq = request.getParameter("qq");
        String email = request.getParameter("email");
        Contact contact = new Contact();

        contact.setAddress(address);
        contact.setAge(Integer.parseInt(age));
        contact.setEmail(email);
        contact.setName(name);
        contact.setQq(qq);
        contact.setSex(sex);


        boolean flog = iAddService.add(contact);

        if(flog){
            response.sendRedirect("ListServlet");
        }else {
            request.setAttribute("adderr","用户已存在");
            request.getRequestDispatcher("add.jsp").forward(request,response);
        }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           doGet(request,response);
    }
}
